<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Asset_location_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function search($loc, $desc)
    {
    	$var = '';
    	$count = 0;
    	if(!empty($loc)){
    		$var .= ($count > 0 ? ' AND ' : '')."location_code LIKE '".$loc."' ";
    		$count++;
    	}

    	if(!empty($desc)){
    		$var .= ($count > 0 ? ' AND ' : '')."description LIKE '".$desc."' ";
    		$count++;
    	}

    	return $this->db->query('SELECT * FROM fam_locations WHERE '.$var)->result();
    }

    public function getData($id){
    	$this->db->where('location_code',$id);
    	return $this->db->get('fam_locations')->result();
    }

    public function insert_data($data){
    	$this->db->query('call fam_locations_ins(?,?,?,?,@err,@msg)',$data);
    	$que = $this->db->query("SELECT @err as err,@msg as msg");
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$err = $keys->err;
			$msg = $keys->msg;
		}
		$arr = array(
			'err' => $err,
			'msg' => $msg,
		);
		return $arr;
    }

    public function update_data($data){
    	$this->db->query('call fam_locations_upd(?,?,?,?,@err,@msg)',$data);
    	$que = $this->db->query("SELECT @err as err,@msg as msg");
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$err = $keys->err;
			$msg = $keys->msg;
		}
		$arr = array(
			'err' => $err,
			'msg' => $msg,
		);
		return $arr;
    }
}